package com.caigou.modules.famous.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.famous.bean.FamousBean
import com.caigou.modules.famous.repository.FamousRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/09
 */
@HiltViewModel
class FamousViewModel @Inject constructor(private val repository: FamousRepository) : ViewModel() {

    val teXiaoListLiveData = MutableLiveData<Resource<FamousBean>>()
    val teWeiListLiveData = MutableLiveData<Resource<FamousBean>>()
    val pingTeListLiveData = MutableLiveData<Resource<FamousBean>>()
    val banShuListLiveData = MutableLiveData<Resource<FamousBean>>()
    val otherListLiveData = MutableLiveData<Resource<FamousBean>>()
    val shaBangListLiveData = MutableLiveData<Resource<FamousBean>>()
    val subscribeLiveData = MutableLiveData<Resource<String>>()
    val likeLiveData = MutableLiveData<Resource<String>>()

    fun getTeXiaoFamousList(dareTypeId: Int, type: Int, sort: Int, currentUserId: String) {
        teXiaoListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getFamousList(dareTypeId, type, sort, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<FamousBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    teXiaoListLiveData.value = it
                }
        }
    }

    fun getTeWeiFamousList(dareTypeId: Int, type: Int, sort: Int, currentUserId: String) {
        teWeiListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getFamousList(dareTypeId, type, sort, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<FamousBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    teWeiListLiveData.value = it
                }
        }
    }

    fun getPingTeFamousList(dareTypeId: Int, type: Int, sort: Int, currentUserId: String) {
        pingTeListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getFamousList(dareTypeId, type, sort, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<FamousBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    pingTeListLiveData.value = it
                }
        }
    }

    fun getBanShuFamousList(dareTypeId: Int, type: Int, sort: Int, currentUserId: String) {
        banShuListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getFamousList(dareTypeId, type, sort, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<FamousBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    banShuListLiveData.value = it
                }
        }
    }

    fun getOtherFamousList(dareTypeId: Int, type: Int, sort: Int, currentUserId: String) {
        otherListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getFamousList(dareTypeId, type, sort, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<FamousBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    otherListLiveData.value = it
                }
        }
    }

    fun getShaBangFamousList(dareTypeId: Int, type: Int, sort: Int, currentUserId: String) {
        shaBangListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getFamousList(dareTypeId, type, sort, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<FamousBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    shaBangListLiveData.value = it
                }
        }
    }

    fun setLike(dareContentId: Int, type: Int, senderId: String, receiverId: String) {
        likeLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setLike(dareContentId, type, senderId, receiverId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    likeLiveData.value = it
                }
        }
    }

    fun setSubscribe(dareTypeId: Int, type: Int, subscriberId: String, receiverId: String) {
        subscribeLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setSubscribe(dareTypeId, type, subscriberId, receiverId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    subscribeLiveData.value = it
                }
        }
    }
}